package houxb.cn;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SVNClient {
    private final String SVN_URL = "http://192.168.200.132/clib/";
    private final String SVN_USERNAME = "houxiaobiao";
    private final String SVN_PASSWD = "123456";

    public static String decrypt(Object obj) {
        String str;
        if (obj == null || (str = obj.toString()).trim().isEmpty()) {
            return "";
        }
        return new String(Base64.decodeBase64(str.getBytes()));
    }

    private SVNRepository getRepository(String dirPath, String docCode, String name, String pwd) {
        FSRepositoryFactory.setup();
        SVNRepository repository;
        String svnPath = SVN_URL + docCode;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnPath));
        } catch (SVNException e) {
            return null;
        }
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, pwd.toCharArray());
        repository.setAuthenticationManager(authManager);
        SVNNodeKind nodeKind;
        try {
            nodeKind = repository.checkPath(dirPath, -1);
        } catch (SVNException e) {
            log.error("SVN连接失败，请确认文档库" + docCode + "是否存在！");
            return null;
        }
        if (nodeKind == SVNNodeKind.NONE) {
            log.error("文档库不存在，{}", docCode);
            return null;
        }
        return repository;
    }

    public static void listEntries(SVNRepository repository, String path, List<String> files) throws SVNException {
        List<SVNDirEntry> entries = new ArrayList<>();
        repository.getDir(path, -1, null, entries);
        for (SVNDirEntry entry : entries) {
            System.out.println("/" + (path.equals("") ? "" : path + "/") + entry.getName() +
                    " ( author: '" + entry.getAuthor() + "'; REVISION: " + entry.getRevision() +
                    "; date: " + entry.getDate() + ")");
            if (entry.getKind() == SVNNodeKind.DIR) {
                listEntries(repository, (path.isEmpty()) ? entry.getName() : path + "/" + entry.getName(), files);
            } else if (entry.getKind() == SVNNodeKind.FILE) {
                files.add((path.isEmpty() ? "" : path + "/") + entry.getName());
            }
        }
    }

    public void execute(String param) throws SVNException {
        // 根据需求编号获取年份
        String dirPath = "/trunk/2023/REQM2023_1662_数据云前台页面优化";
        SVNRepository repository = getRepository(dirPath, "903qmsdoc", SVN_USERNAME, SVN_PASSWD);
        if (repository != null) {
            List<String> files = new ArrayList<>();
            listEntries(repository, dirPath, files);
            System.out.println(String.join("\r\n", files));
        }
    }

    public static void main(String[] args) throws SVNException {
        SVNClient svnClient = new SVNClient();
        svnClient.execute(null);
        Map<String, List<String>> map = new HashMap<>();
        map.computeIfAbsent("S", k -> new ArrayList<>());
    }
}
